package com.jader.adapter;

import java.util.ArrayList;
import java.util.List;

import com.jader.beans.Response;
import com.jader.util.JaderApplication;
import com.lidroid.xutils.BitmapUtils;
import com.zlp.jader.R;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class MyTendsAdapter extends BaseAdapter {
	// 数据来源
	List<Response> list = new ArrayList<Response>();
	Context context;// 接收上下文参数，确定是哪个布局在使用
	private LayoutInflater inflater;// 初始化布局文件，把XML布局文件转化为android中的View
	private BitmapUtils bitmapUtils;
	public MyTendsAdapter(List<Response> list, Context context) {
		super();
		this.list = list;
		this.context = context;
		inflater = LayoutInflater.from(context);
		bitmapUtils =JaderApplication.getBitmapUtils();
		bitmapUtils.configDefaultLoadingImage(R.drawable.pic_item_list_default);
	}

	@Override
	public int getCount() {
		// 数据总量
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		// 返回每一行的数据
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	ViewHolder viewHolder;

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// position:列表的索引，从0开始；convertView是每一行的布局；parent每一行的父布局，没有则写null
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.listview_response, null);
			// 初始化每一行的控件
			viewHolder = new ViewHolder();
			viewHolder.img_response_pic = (ImageView) convertView
					.findViewById(R.id.img_listview_response_pic);
			viewHolder.tv_response_name = (TextView) convertView
					.findViewById(R.id.tv_listview_response_responsename);
			viewHolder.tv_response_text = (TextView) convertView
					.findViewById(R.id.tv_listview_response_responsetext);
			viewHolder.tv_response_date = (TextView) convertView
					.findViewById(R.id.tv_listview_response_responsedate);
			// 首次调用getView，创建viewHolder实例，初始化控件，并把这个实例缓存到界面上
			convertView.setTag(viewHolder);
		} else {
			// 不是第一次调用，直接取出缓存中的viewholder即可
			viewHolder = (ViewHolder) convertView.getTag();
		}
		// 指定每一个布局对应的数据
		String redID = list.get(position).getPicID();
		bitmapUtils.display(viewHolder.img_response_pic, JaderApplication.getIP()+"/Jader"+redID);
		String response_name = list.get(position).getResponse_name();
		viewHolder.tv_response_name.setText(response_name);
		String response_text = list.get(position).getResponse_text();
		viewHolder.tv_response_text.setText(response_text);
		String response_date = list.get(position).getResponse_date();
		viewHolder.tv_response_date.setText(response_date);
		return convertView;
	}

	class ViewHolder {
		// 做界面控件初始化缓存工作
		ImageView img_response_pic;
		TextView tv_response_name, tv_response_text, tv_response_date;
	}
}
